package com.webtest.mvctest2.controller;

import com.webtest.mvctest2.dao.factory.CustomerDaoFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2018/7/29 13:40
 */
public class Initservlet extends HttpServlet {

    private static final long serialVersionUID = 346756255019955968L;

    @Override
    public void init() throws ServletException {
        System.out.println("[Init] Start init servlet....");
        //读取资源路径下的switch.properties文件
        InputStream in = getClass().getClassLoader().getResourceAsStream("swtich.properties");
        Properties properties = new Properties();
        try {
            properties.load(in);
            // 获取swtich.properties中的type属性值
            String property = properties.getProperty("type", "jdbc");
            // 赋值给了CustomerDaoFactory的type属性
            CustomerDaoFactory.getInstance().setType(property);
            System.out.println("[Init] Set dao type as " + property);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
